package com.beauty.product.controller;

import com.beauty.product.dto.CategoryDto;
import com.beauty.product.result.Result;
import com.beauty.product.service.CategoryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class CategoryController {
    
    private final CategoryService categoryService;
    
    @PostMapping
    public Result<CategoryDto> createCategory(@Valid @RequestBody CategoryDto categoryDto) {
        return Result.success(categoryService.createCategory(categoryDto));
    }
    
    @GetMapping("/{id}")
    public Result<CategoryDto> getCategoryById(@PathVariable Long id) {
        return Result.success(categoryService.getCategoryById(id));
    }
    
    @GetMapping("/name/{name}")
    public Result<CategoryDto> getCategoryByName(@PathVariable String name) {
        return Result.success(categoryService.getCategoryByName(name));
    }
    
    @GetMapping("/root")
    public Result<List<CategoryDto>> getRootCategories() {
        return Result.success(categoryService.getRootCategories());
    }
    
    @GetMapping("/sub/{parentId}")
    public Result<List<CategoryDto>> getSubcategories(@PathVariable Long parentId) {
        return Result.success(categoryService.getSubcategories(parentId));
    }
    
    @GetMapping("/level/{level}")
    public Result<List<CategoryDto>> getCategoriesByLevel(@PathVariable Integer level) {
        return Result.success(categoryService.getCategoriesByLevel(level));
    }
    
    @GetMapping
    public Result<List<CategoryDto>> getAllCategories() {
        return Result.success(categoryService.getAllCategories());
    }
    
    @GetMapping("/tree")
    public Result<List<CategoryDto>> getCategoryTree() {
        return Result.success(categoryService.getCategoryTree());
    }
    
    @PutMapping("/{id}")
    public Result<CategoryDto> updateCategory(@PathVariable Long id, @Valid @RequestBody CategoryDto categoryDto) {
        return Result.success(categoryService.updateCategory(id, categoryDto));
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }
} 